package com.nymph.executor.parameter;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Description: This is a sample class.
 * @Author: Simon
 */
public class DefaultParameterHandler implements ParameterHandler{

    private Object parameter;

    public DefaultParameterHandler(Object parameter){
        this.parameter=parameter;
    }

    //将sql 参数设置到preparedStatement中
    @Override
    public void setParameters(PreparedStatement preparedStatement) {
        try {
            if (parameter!=null){
                if (parameter.getClass().isArray()){
                    Object[] params=(Object[])parameter;
                    for (int i = 0; i < params.length; i++) {
                        //Mapper保证传入参数类型匹配，这里就不做类型转换了
                        preparedStatement.setObject(i+1,params);
                    }
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
